package com.vpx.server.framework.base;

import com.vpx.server.bean.enums.Status;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashMap;
import java.util.Map;

/**
 * @author lihui
 * @title: vpx
 * @module TODO
 * @description: 应答对象
 * @date 2021/9/19 9:01
 */
@ApiModel
public class ResultMsg<T> extends Base {
    private final static Logger logger = LoggerFactory.getLogger(ResultMsg.class);

    public static final String DEFAULT_SUCCESS_CODE = "1";

    public static final String DEFAULT_FAIL_CODE = "0";

    /**
     * 未登录
     */
    public static final String UNLOGIN_CODE = "-1";

    /**
     * 未授权
     */
    public static final String UNAUTH_CODE = "-2";

    /**
     * sid参数为空
     */
    public static final String SID_NULL_CODE = "-3";

    /**
     * SID过期
     */
    public static final String SID_EXPIRE = "-4";

    public static final String EXCEPTION_CODE = "9999";

    private static final Map<String, String> MESSAGE_DESC = new HashMap<String, String>();

    static {
        MESSAGE_DESC.put(UNLOGIN_CODE, "登录失效");
        MESSAGE_DESC.put(UNAUTH_CODE, "未授权");
        MESSAGE_DESC.put(DEFAULT_SUCCESS_CODE, "操作成功");
        MESSAGE_DESC.put(DEFAULT_FAIL_CODE, "操作失败");
        MESSAGE_DESC.put(EXCEPTION_CODE, "服务器繁忙，请稍后重试");
        MESSAGE_DESC.put(SID_NULL_CODE, "sid参数为空");
        MESSAGE_DESC.put(SID_EXPIRE, "sid过期");
    }

    public static String getDefaultCodeMsg(String code) {
        return MESSAGE_DESC.get(code);
    }


    /**
     * 结果状态
     */
    protected Status status;

    /**
     * 状态
     */
    @ApiModelProperty("状态")
    private Boolean success;

    /**
     * 结果码
     */
    @ApiModelProperty("结果码")
    private String code;

    /**
     * 应答信息
     */
    @ApiModelProperty("应答信息")
    private String message;

    /**
     * 返回详情数据
     */
    @ApiModelProperty("返回详情数据")
    private T data;

    private long ctime = System.currentTimeMillis();

    public ResultMsg() {
        this.code = "1";
        this.success = true;
        this.status = Status.SUCCESS;
        this.message = "操作成功";
        logger.info("Ajax请求应答>>>状态：{}，code：{}，message：{}，data：{}", success, code, message, data);
    }
       

    public ResultMsg(boolean success, String code, String message, T data) {
        this.code = code;
        this.success = success;
        this.message = message;
        this.data = data;
        if(success) {
        	this.status = Status.SUCCESS;
        }else {
        	this.status = Status.FAIL;
        }
        logger.info("Ajax请求应答>>>状态：{}，code：{}，message：{}，data：{}", success, code, message, data);
    }

    public static ResultMsg success(Object data) {
        return new ResultMsg(true, DEFAULT_SUCCESS_CODE, getDefaultCodeMsg(DEFAULT_SUCCESS_CODE), data);
    }

    public static ResultMsg success(Object data, String message) {
        return new ResultMsg(true, DEFAULT_SUCCESS_CODE, message, data);
    }

    public static ResultMsg fail(Object data, String code, String message) {
        return new ResultMsg(false, code, message, data);
    }

    public static ResultMsg fail(String code, String message) {
        return new ResultMsg(false, code, message, null);
    }


    public static ResultMsg success() {
        return new ResultMsg(true, DEFAULT_SUCCESS_CODE, getDefaultCodeMsg(DEFAULT_SUCCESS_CODE), null);
    }

    public static ResultMsg fail(String message) {
        return new ResultMsg(false, DEFAULT_FAIL_CODE, message, null);
    }


    public boolean isSuccess() {
        return this.status == Status.SUCCESS;
    }

    public void setStatus(Status status) {
        this.status = status;
    }

    public void setSuccess(boolean success) {
        this.success = success;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public long getCtime() {
        return ctime;
    }

    public void setCtime(long ctime) {
        this.ctime = ctime;
    }

    public static void main(String[] args) {
		ResultMsg ms = ResultMsg.success();
		System.out.println(ms.isSuccess());
	}
}
